<template>
  <div class="manage">
    <div v-if="editMode == 0">
      <el-dialog
        title="仓库进货"
        :visible.sync="dialogVisible"
        width="39%"
        :before-close="handleClose"
      >
        <!-- 表单信息 -->
        <el-form
          ref="form"
          :rules="rules"
          :model="supplyInfoForm"
          label-width="100px"
        >
          <el-form-item label="药品名称" prop="did">
            <el-select
              @change="drugSelectChanged(supplyInfoForm.did)"
              v-model="supplyInfoForm.did"
              placeholder="请选择药品"
            >
              <div v-for="drug in drugs" :key="drug.did">
                <el-option
                  v-if="form.did == drug.did"
                  :label="drug.drugName"
                  :value="drug.did"
                  selected
                ></el-option>
                <el-option
                  v-else="form.did == drug.did"
                  :label="drug.drugName"
                  :value="drug.did"
                ></el-option>
              </div>
            </el-select>
            <el-form :model="searchForm" class="searchForm">
              <el-form-item class="search">
                <el-input
                  placeholder="药品名过滤"
                  v-model="searchForm.drugName"
                ></el-input>
                <el-button type="primary" @click="searchNameByDrugName"
                  >查询</el-button
                >
              </el-form-item>
            </el-form>
          </el-form-item>
          <el-form-item label="厂商名">
            <el-input
              placeholder="厂商名"
              v-model="selectSupplierName"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="供应量" prop="splyNum">
            <el-input
              placeholder="请输入供应量"
              v-model="supplyInfoForm.splyNum"
            ></el-input>
          </el-form-item>
          <el-form-item label="生产日期" prop="proTime1">
            <el-col :span="11">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="supplyInfoForm.proTime1"
                style="width: 100%"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-col>
            <el-col :span="11">
              <el-time-picker
                placeholder="选择时间"
                v-model="supplyInfoForm.proTime2"
                style="width: 100%"
                value-format="HH:mm:ss"
              ></el-time-picker>
            </el-col>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <div v-else-if="editMode == 1">
      <el-dialog
        title="仓库记录编辑"
        :visible.sync="dialogVisible"
        width="39%"
        :before-close="handleClose"
      >
        <el-form
          ref="form"
          :rules="rules"
          :model="form"
          label-width="100px"
          class="editDialog"
        >
          <el-form-item label="在售">
            <div>
              <el-switch v-model="form.isSale"></el-switch>
            </div>
          </el-form-item>
          <el-form-item label="药品名">
            <el-input
              placeholder="药品名"
              v-model="form.drugName"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="厂商名">
            <el-input
              placeholder="厂商名"
              v-model="form.splName"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="库存" prop="stock">
            <el-input placeholder="请输入库存" v-model="form.stock"></el-input>
          </el-form-item>
          <el-form-item label="已售数量" prop="sellNum">
            <el-input
              placeholder="请输入已售数量"
              v-model="form.sellNum"
            ></el-input>
          </el-form-item>
          <el-form-item label="退货量" prop="retNum">
            <el-input
              placeholder="请输入退货量"
              v-model="form.retNum"
            ></el-input>
          </el-form-item>
          <el-form-item label="售价" prop="goodsPrice">
            <el-input
              placeholder="请输入售价"
              v-model="form.goodsPrice"
            ></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <div v-else>
      <el-dialog
        title="提交异常药品"
        :visible.sync="dialogVisible"
        width="39%"
        :before-close="handleClose"
      >
        <el-form
          ref="form"
          :rules="rules"
          :model="drugProblemInfoForm"
          label-width="100px"
          class="editDialog"
        >
          <el-form-item label="药品名">
            <el-input
              placeholder="药品名"
              v-model="drugProblemInfoForm.drugName"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="厂商名">
            <el-input
              placeholder="厂商名"
              v-model="drugProblemInfoForm.splName"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="库存">
            <el-input
              placeholder="库存"
              v-model="drugProblemInfoForm.stock"
              :disabled="true"
            ></el-input>
          </el-form-item>
          <el-form-item label="问题数量" prop="dpNum">
            <el-input
              placeholder="请输入数量"
              v-model="drugProblemInfoForm.dpNum"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="问题类型"
            class="searchFormSelect"
            prop="problemType"
          >
            <el-select
              v-model="drugProblemInfoForm.problemType"
              placeholder="请选择问题类型"
              style="width: 100%"
            >
              <el-option
                v-for="item in problemList"
                :key="item.typeId"
                :label="item.typeName"
                :value="item.typeId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <div class="manage-header">
      <el-button class="addBtn" type="primary" @click="handlerAdd">
        + 进货
      </el-button>
      <el-form :model="searchForm" class="searchForm">
        <el-form-item label="选择" class="searchFormSelect">
          <el-select
            v-model="searchForm.searchType"
            placeholder="请选择搜索类型"
            style="width: 100%"
          >
            <el-option label="药品名" :value="0" selected></el-option>
            <el-option label="厂商名" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="search">
          <el-input
            placeholder="请输入名称"
            v-model="searchForm.name"
          ></el-input>
          <el-button type="primary" @click="searchName">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="common-table">
      <el-table height="90%" :data="tableData" stripe style="width: 100%">
        <el-table-column prop="drugName" label="药品名"> </el-table-column>
        <el-table-column prop="splName" label="供应商"> </el-table-column>
        <el-table-column :min-width="30" prop="stock" label="库存">
        </el-table-column>
        <el-table-column :min-width="30" prop="sellNum" label="已售数量">
        </el-table-column>
        <el-table-column :min-width="30" prop="retNum" label="退货数">
        </el-table-column>
        <el-table-column :min-width="50" prop="drugPrice" label="进价(RMB)">
        </el-table-column>
        <el-table-column :min-width="50" prop="goodsPrice" label="售价(RMB)">
        </el-table-column>
        <el-table-column :min-width="50" prop="isSale" label="状态">
          <template slot-scope="scope">{{
            scope.row.isSale ? "在售" : "停售"
          }}</template>
        </el-table-column>
        <el-table-column :min-width="100" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="warning"
              @click="submitProblem(scope.row)"
              >异常</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDel(scope.row.wid)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="pageData.total"
        @current-change="handlePage"
        :page-size="onePageSize"
        :current-page.sync="pageData.curPage"
        class="pager"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  getWarehouse,
  whPrchsAdd,
  editWarehouse,
  delWarehouse,
  submitDrugProblem,
} from "@/api/warehouseInfo/warehouse";
import { getSupplier, getDrug } from "@/api/common";
import { getSupplierName } from "@/api/drugInfo/drug";
import { getTypeList } from "@/api/warehouseInfo/drugProblemInfo";
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        wid: "",
        did: "",
        stock: "",
        sellNum: "",
        retNum: "",
        drugPrice: "",
        goodsPrice: "",
        isSale: "",
      },
      formNull: {
        wid: "",
        did: "",
        stock: "",
        sellNum: "",
        retNum: "",
        drugPrice: "",
        goodsPrice: "",
        isSale: "",
      },
      supplyInfoForm: {
        did: "",
        splyNum: "",
        proTime1: "",
        proTime2: "",
      },
      supplyInfoFormNull: {
        did: "",
        splyNum: "",
        proTime1: "",
        proTime2: "",
      },
      drugProblemInfoForm: {
        wid: "",
        drugName: "",
        splName: "",
        dpNum: "",
        stock: "",
        problemType: "",
      },
      drugProblemInfoFormNull: {
        wid: "",
        drugName: "",
        splName: "",
        dpNum: "",
        stock: "",
        problemType: "",
      },
      rules: {
        did: [
          {
            required: true,
            message: "请选择药品名称",
            trigger: "change",
            type: "number",
          },
        ],
        splyNum: [{ required: true, message: "请输入供应量", trigger: "blur" }],
        proTime1: [
          { required: true, message: "请输入生产日期", trigger: "blur" },
        ],
        stock: [{ required: true, message: "请输入库存", trigger: "blur" }],
        sellNum: [{ required: true, message: "请输入销售量", trigger: "blur" }],
        retNum: [{ required: true, message: "请输入退货量", trigger: "blur" }],
        goodsPrice: [
          { required: true, message: "请输入售价", trigger: "blur" },
        ],
        dpNum: [{ required: true, message: "请输入数量", trigger: "blur" }],
        problemType: [
          {
            required: true,
            message: "请输入类型",
            trigger: "blur",
            type: "number",
          },
        ],
      },
      tableData: [
        {
          wid: "",
          did: "",
          stock: "",
          sellNum: "",
          retNum: "",
          goodsPrice: "",
          drugPrice: "",
          drugName: "",
          splName: "",
          isSale: "",
        },
      ],
      modelType: 0, //0表示新增 1表示编辑 2表示提交异常药品
      editMode: 1, //1表示编辑模式（控制Dialog）2表示填写异常信息
      onePageSize: 10,
      pageData: {
        total: 0, //当前总条数
        curPage: 1,
      },
      searchForm: {
        name: "",
        searchType: 0,
        drugName: "",
      },
      selectSupplierName: "请选择药品",
      drugs: [{ did: "1", drugName: "无数据" }],
      problemList: [{ typeId: "1", typeName: "22" }],
    };
  },
  methods: {
    //提交用户表单
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.modelType == 0) {
            //进货
            whPrchsAdd({
              did: this.supplyInfoForm.did,
              splyNum: this.supplyInfoForm.splyNum,
              proTime:
                this.supplyInfoForm.proTime1 +
                "T" +
                this.supplyInfoForm.proTime2,
            }).then(() => {
              //发送后的操作
              this.$refs.form.clearValidate(["did"]);
              this.getList();
            });
          } else if (this.modelType == 1) {
            editWarehouse(this.form).then(() => {
              //更新后的操作
              this.getList();
            });
          } else if (this.modelType == 2) {
            submitDrugProblem(this.drugProblemInfoForm).then(({ data }) => {
              //提交问题药品后的操作
              this.getList();
            });
          }
          //修改页码为第一页
          this.pageData.curPage = 1;
          //关闭弹窗
          this.dialogVisible = false;
          //清除表单信息
          if (this.editMode == 0)
            this.supplyInfoForm = JSON.parse(
              JSON.stringify(this.supplyInfoFormNull)
            );
          else if (this.editMode == 1)
            this.form = JSON.parse(JSON.stringify(this.formNull));
          else if (this.editMode == 2)
            this.drugProblemInfoForm = JSON.parse(
              JSON.stringify(this.drugProblemInfoFormNull)
            );
        }
      });
    },
    handleClose() {
      // if (this.editMode == 1) this.$refs.form.resetFields();
      if (this.editMode == 0) {
        this.supplyInfoForm = JSON.parse(
          JSON.stringify(this.supplyInfoFormNull)
        );
        this.$refs.form.clearValidate();
      } else if (this.editMode == 1) {
        this.form = JSON.parse(JSON.stringify(this.formNull));
      }
      // this.$refs.supplyInfoForm.clearValidate();
      else if (this.editMode == 2) {
        this.drugProblemInfoForm = JSON.parse(
          JSON.stringify(this.drugProblemInfoFormNull)
        );
      }
      this.dialogVisible = false;
    },
    cancel() {
      this.handleClose();
    },
    handleEdit(row) {
      //深拷贝
      this.form = JSON.parse(JSON.stringify(row));
      this.modelType = 1;
      this.editMode = 1;
      this.dialogVisible = true;
    },
    submitProblem(row) {
      var tempForm = JSON.parse(JSON.stringify(row));
      this.drugProblemInfoForm.drugName = tempForm.drugName;
      this.drugProblemInfoForm.splName = tempForm.splName;
      this.drugProblemInfoForm.stock = tempForm.stock;
      this.drugProblemInfoForm.wid = tempForm.wid;
      this.modelType = 2;
      this.editMode = 2;
      this.dialogVisible = true;
    },
    handleDel(wid) {
      this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delWarehouse(wid).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
            //修改页码为第一页
            this.pageData.curPage = 1;
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    getProblemList() {
      getTypeList().then(({ data }) => {
        this.problemList = data.data;
      });
    },
    getList(searchType, curPage, limit, sname = null) {
      this.getProblemList();
      if (curPage === null || limit == null) {
        curPage = 1;
        limit = this.onePageSize;
      }
      getWarehouse({
        type: searchType,
        curPage: curPage,
        limit: limit,
        sname: sname,
      }).then(({ data }) => {
        //获取数据
        const page = data.data;
        this.tableData = page.records;
        this.pageData.total = page.total;
      });
      //更新下拉框数据
      getSupplier().then(({ data }) => {
        this.suppliers = data.data;
      });
      getDrug().then(({ data }) => {
        this.drugs = data.data;
      });
    },
    searchName() {
      this.getList(
        this.searchForm.searchType,
        null,
        null,
        this.searchForm.name
      );
    },
    searchNameByDrugName() {
      getDrug(this.searchForm.drugName).then(({ data }) => {
        this.drugs = data.data;
      });
    },
    handlerAdd() {
      this.editMode = 0;
      this.modelType = 0;
      this.dialogVisible = true;
    },
    //选择页面的回调
    handlePage(val) {
      this.getList(this.searchForm.searchType, val, this.onePageSize);
    },
    drugSelectChanged(did) {
      getSupplierName({ did: did }).then(({ data }) => {
        this.selectSupplierName = data.data;
      });
    },
  },

  mounted() {
    this.getList();
  },
};
</script>

<style lang="less" scoped>
.manage {
  height: 100%;
  .manage-header {
    display: flex;
    justify-content: space-between;
    .addBtn {
      height: 70%;
    }
    .searchFormSelect {
      width: 200px;
    }
  }
  .common-table {
    position: relative;
    height: calc(85%);
    .pager {
      position: absolute;
      bottom: 0;
      right: 20px;
    }
  }
  .searchForm {
    display: flex;
  }
  .editDialog {
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    flex-direction: column;
  }
}
</style>
